/*
 * @Author: Clark
 * @Email: haixuanwoTxh@gmail.com
 * @Date: 2023-12-27 00:30:12
 * @LastEditors: Clark
 * @LastEditTime: 2024-01-07 13:29:58
 * @Description: file content
 */

#include "tomatoclock.h"

#include <iostream>
using std::cerr;
using std::cout;
using std::endl;

#include <fstream>
using std::ifstream;

#include <string>
using std::string;

#include <stdexcept>
using std::runtime_error;

TomatoClock::TomatoClock(const QString &infoFileName, const QString &dataFileName)
{
    infoFile = std::make_shared<QFile>(infoFileName);
    dataFile = std::make_shared<QFile>(dataFileName);
    tomatoClockData = std::make_shared<TomatoClockData>();
}

TomatoClock::~TomatoClock()
{
    infoFile->close();
    dataFile->close();
}

bool TomatoClock::init()
{
    if (!infoFile->open(QIODevice::ReadWrite | QIODevice::Unbuffered))
    {
        qDebug()<<"open infoFile fail";
        return false;
    }

    if (!dataFile->open(QIODevice::ReadWrite | QIODevice::Unbuffered | QIODevice::Append))
    {
        qDebug()<<"open dataFile fail";
        return false;
    }

    QDateTime time = QDateTime::currentDateTime();
    QString date = time.toString("yyyy-MM-dd");

    QByteArray clockInfo = infoFile->readAll();
    if (clockInfo.size() < sizeof(uint64_t))
    {
        allDays = 1;
        tomatoClockData->count = 0;
        tomatoClockData->startPos = 0;
        memcpy(tomatoClockData->date, date.toStdString().c_str(), date.size());
        tomatoClockData->date[date.size()] = 0;

        infoFile->write((char*)&allDays, sizeof(allDays));
        infoFile->write((char*)tomatoClockData.get(), sizeof(TomatoClockData));
        qDebug()<<"allDays size: "<<sizeof(allDays)<<"TomatoClockData size: "<<sizeof(TomatoClockData)
               <<"date.size: "<<date.size()<<date;

        qDebug()<<"this is origin file allDays: 0 startPos 0"<< "date: "<<date;
        return true;
    }

    allDays = *((uint64_t *)(clockInfo.data()));
    qDebug()<<"read allDays:"<<allDays;

    int index = sizeof(uint64_t);
    index += ((allDays-1)*sizeof(TomatoClockData)); // 跳到最后一天
    qDebug()<<"index: "<<index;

    memcpy(tomatoClockData.get(), clockInfo.data()+index, sizeof(TomatoClockData));
    index += sizeof(tomatoClockData);

    QString tomatoClockDate(tomatoClockData->date);

    qDebug() << "index: "<<index<<" date:"<<tomatoClockDate<<" count: "<<tomatoClockData->count<<" startPos:"<<tomatoClockData->startPos;

    if (0 != memcmp(tomatoClockData->date, date.toStdString().c_str(), date.size()))
    {
        allDays++;
        tomatoClockData->startPos =
                (tomatoClockData->startPos + tomatoClockData->count * 12);

        memcpy(tomatoClockData->date, date.toStdString().c_str(), date.size());
        tomatoClockData->count = 0;

        infoFile->seek(0);
        infoFile->write((char*)&allDays, sizeof(allDays));
        infoFile->seek((allDays-1)*sizeof(TomatoClockData) + sizeof(allDays));
        infoFile->write((char*)tomatoClockData.get(), sizeof(TomatoClockData));
        qDebug() << "T --- new day index: "<<index<<" date:"<<date<<" count: "<<tomatoClockData->count<<" startPos:"<<tomatoClockData->startPos;
    }

    return true;
}

bool TomatoClock::add_clock()
{
    char startTime[12] = {0};
    QDateTime time = QDateTime::currentDateTime();
    QString date = time.toString("yyyy-MM-dd");
    QString timePoint = time.toString("hh:mm:ss");
    memcpy(startTime, timePoint.toStdString().c_str(), timePoint.size());

    if (0 == memcmp(tomatoClockData->date, date.toStdString().c_str(), date.size()))
    {
        tomatoClockData->count++;
        qDebug()<<"T --- the same day count: "<<tomatoClockData->count;
    }
    else
    {
        qDebug()<<"T --- the old day:"<<QString(tomatoClockData->date);

        allDays++;
        tomatoClockData->startPos =
                (tomatoClockData->startPos + tomatoClockData->count * sizeof(startTime));

        memcpy(tomatoClockData->date, date.toStdString().c_str(), date.size());
        tomatoClockData->count = 1;

        infoFile->seek(0);
        infoFile->write((char*)&allDays, sizeof(allDays));
    }

    qDebug()<<"T --- add clock: "<<startTime<<"date: "<<tomatoClockData->date<<"allDays: "<<allDays
           <<" count: "<<tomatoClockData->count;

    dataFile->write(startTime, sizeof(startTime));

    infoFile->seek((allDays-1)*sizeof(TomatoClockData) + sizeof(allDays));
    infoFile->write((char*)tomatoClockData.get(), sizeof(TomatoClockData));

    return true;
}


uint32_t TomatoClock::get_clock_count()
{
    return tomatoClockData->count;
}
